#include "StdAfx.h"
#include "im_helper.h"
#include "dll_object.h"
#include "sm_driver.h"
#include "im_packet_impl.h"
#include "im_protocol_impl.h"
#include <ObjBase.h>

im_helper::im_helper(void)
{
}

im_helper::~im_helper(void)
{
}

aot_innerid_t 
im_helper::user_id_to_inner_id(const char* user_id)
{
	if( get_curr_user()->id() == user_id )
	{
		return get_curr_user()->inner_id();
	}

	aot_innerid_t id = (aot_innerid_t)g_dll_obj->user_lst_mgr_->get_user_inner_id(user_id);
	if( id != 0 )
	{
		return id;
	}

	id = (aot_innerid_t)g_dll_obj->enterprise_op_->get_user_inner_id(user_id);
	if( id != 0 )
	{
		return id;	
	}

	id = g_dll_obj->im_user_mgr_.user_id_to_inner_id(user_id);
	return id;
}

std::string 
im_helper::inner_id_to_user_id(aot_innerid_t inner_id)
{
	if( 0 == inner_id )
	{
		return "";
	}
	if( inner_id == get_curr_user()->inner_id() )
	{
		return get_curr_user()->id();
	}
	if( 1 )
	{
		aot::tt::iobj_auto_ptr<aot::tt::istr> s;
		g_dll_obj->user_lst_mgr_->inner_id_to_user_id(inner_id, s);
		if( s->length() > 0 )
		{
			return s->c_str();
		}
	}
	if( 1 )
	{
		aot::tt::iobj_auto_ptr<aot::tt::istr> s;
		g_dll_obj->enterprise_op_->inner_id_to_user_id(inner_id, s);
		if( s->length() > 0 )
		{
			return s->c_str();
		}
	}
	if( 1 )
	{
		std::string s2 = g_dll_obj->im_user_mgr_.inner_id_to_user_id(inner_id);
		if( s2.length() > 0 )
		{
			return s2;
		}
	}
	return "";
}

aot_innerid_t 
im_helper::get_myself_inner_id()
{
	return get_curr_user()->inner_id();
}

bool 
im_helper::send(aot::im::iim_packet* im_pkt)
{
	im_protocol_impl prot;
	aot::inet::ipacket* inet_pkt = NULL;
	if( !prot.build(im_pkt, &inet_pkt) )
	{
		aot_log_error(AOT_LM_ERROR, "send() failed");
		return false;
	}

	return get_sm_driver()->channel_.send(inet_pkt);
}

//aot::im::iim_packet* 
//im_helper::build_reply_with(aot::im::iim_packet* src, int status_code)
//{
//	aot::im::iim_packet* pkt = im_packet_impl::create_pkt();
//	if( NULL == pkt )
//	{
//		return NULL;
//	}
//
//	return pkt;
//}


void 
im_helper::fill_from_info_with_myself(aot::im::iim_packet* im_pkt)
{
	im_pkt->exh.from_id = get_myself_inner_id();
	im_pkt->exh.from_attr = 1;
}

void 
im_helper::fill_to_info_with_user_id(aot::im::iim_packet* im_pkt, const char* to_user_id)
{
	im_pkt->exh.to_id = user_id_to_inner_id(to_user_id);
	im_pkt->exh.to_attr = 1;
}

void 
im_helper::fill_to_info_with_user_inner_id(aot::im::iim_packet* im_pkt, aot_uint32_t to_user_inner_id)
{
	im_pkt->exh.to_id = to_user_inner_id;
	im_pkt->exh.to_attr = 1;
}

void 
im_helper::fill_to_info_with_tribe_id(aot::im::iim_packet* im_pkt, const char* tribe_id)
{
	im_pkt->exh.to_id = (aot_uint32_t)atol(tribe_id);
	im_pkt->exh.to_attr = 1;
}

void 
im_helper::fill_to_info_with_dep_id(aot::im::iim_packet* im_pkt, const char* dep_id)
{
	im_pkt->exh.to_id = (aot_uint32_t)atol(dep_id);
	im_pkt->exh.to_attr = 1;
}

void 
im_helper::fill_to_info_with_ims(aot::im::iim_packet* im_pkt)
{
	im_pkt->exh.to_id = get_curr_user()->im_serv_id();
	im_pkt->exh.to_attr = 2;
}

bool 
im_helper::is_reply_packet(aot::im::iim_packet* im_pkt)
{
	return im_pkt->exh.is_reply();
}

void 
im_helper::split_string(std::vector<std::string>* out, const char* buf, size_t buf_len, char token)
{
	if( NULL == buf || buf_len == 0 )
	{
		return;
	}

	std::string val;
	for( size_t i = 0; i < buf_len; ++i )
	{
		if( buf[i] == token )
		{
			out->push_back(val);
			val.clear();
			continue;
		}
		val += buf[i];
	}

	out->push_back(val);
}

std::string
im_helper::make_call_id()
{
	char buf[64] = {0};
	GUID guid;

	::CoCreateGuid(&guid);

	_snprintf_s(buf, sizeof(buf), "%08X-%04X-%04x-%02X%02X-%02X%02X%02X%02X%02X%02X",
		guid.Data1,
		guid.Data2,
		guid.Data3,
		guid.Data4[0], guid.Data4[1],
		guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5],
		guid.Data4[6], guid.Data4[7]);

	return buf;
}

aot_uint32_t 
im_helper::get_user_realtime_ims(const char* user_id)
{
	if( g_dll_obj->enterprise_op_->is_member_in_ent(user_id) || g_dll_obj->buddy_mgr_->find(user_id) )
	{
		return g_dll_obj->user_status_mgr_->get_ims_id(user_id);
	}
	return 0;
}

aot_uint32_t
im_helper::get_tribe_serv_id(const char* tribe_id)
{
	aot_uint32_t sid = 0;
	aot::glb::itribe_info* tribe_info = NULL;
	g_dll_obj->tribe_mgr_->lock();

	tribe_info = g_dll_obj->tribe_mgr_->get_tribe_info(tribe_id);
	if ( tribe_info )
	{
		sid = tribe_info->server_id;
	}
	
	g_dll_obj->tribe_mgr_->unlock();

	return sid;
}
